package worker

import (
	"NovelBook/dao"
	"NovelBook/models/db_models"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func DelBooks(c *gin.Context) {
	idAny, exist := c.Get(auth.WorkerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}
	query := c.Query("id")
	if len(query) == 0 {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}
	bookId, err := strconv.Atoi(query)
	if err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}
	var book db_models.Books
	err = dao.GetDb().Where("id = ?", bookId).Delete(&book).Error
	if err != nil {
		response.FailWithMsg(c, http.StatusBadRequest, response.CodeParamsInvalid, err.Error())
		return
	}
	response.Success(c, nil)
}
